﻿using System;
using System.Data;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmParameter : DockContent
    {
        ParameterClient _wcfParameter;
        RoleRightInfo _listRole;
        ParameterInfo _parameterInfo;
        bool isChange = false;

        public frmParameter()
        {
            InitializeComponent();
            this.KeyPreview = true;
            LoadLanguage();
            _listRole = UIParamater.Quyen_ThamSoHeThong;
            if (!_listRole.Update)
                btnSave.Visible = false;
            else
                btnSave.Visible = true;
            _wcfParameter = Service.Parameter();
            _parameterInfo = new ParameterInfo();
            Info2Form();

        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }
        private void Info2Form()
        {
            DataTable dt = _wcfParameter.Parameter_GetAll().Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                switch (dr["Parameter_Name"].ToString())
                {
                    // 1. mã đơn vị
                    case "UnitCode":
                        txtUnitCode.Text = dr["Parameter_Value"].ToString();
                        txtUnitCode.Tag = dr["Parameter_ID"];
                        break;
                    // 2. Tên đơn vị
                    case "CompanyName":
                        txtCompanyName.Text = dr["Parameter_Value"].ToString();
                        txtCompanyName.Tag = dr["Parameter_ID"];
                        break;
                    // 3. Địa chỉ
                    case "CompanyAddress":
                        txtCompanyAddress.Text = dr["Parameter_Value"].ToString();
                        txtCompanyAddress.Tag = dr["Parameter_ID"];
                        break;
                    // 4. Điện thoại
                    case "Phone":
                        txtPhone.Text = dr["Parameter_Value"].ToString();
                        txtPhone.Tag = dr["Parameter_ID"];
                        break;
                    // 5. Fax
                    case "Fax":
                        txtFax.Text = dr["Parameter_Value"].ToString();
                        txtFax.Tag = dr["Parameter_ID"];
                        break;
                    // 6. Email
                    case "Email":
                        txtEmail.Text = dr["Parameter_Value"].ToString();
                        txtEmail.Tag = dr["Parameter_ID"];
                        break;
                    // 7. mã số thuế
                    case "TaxNo":
                        txtTaxNo.Text = dr["Parameter_Value"].ToString();
                        txtTaxNo.Tag = dr["Parameter_ID"];
                        break;
                    // 8. Số tài khoản
                    case "AccountNo":
                        txtAccountNo.Text = dr["Parameter_Value"].ToString();
                        txtAccountNo.Tag = dr["Parameter_ID"];
                        break;
                    // 9. Tên ngân hàng
                    case "Bank":
                        txtBank.Text = dr["Parameter_Value"].ToString();
                        txtBank.Tag = dr["Parameter_ID"];
                        break;
                    // 10. Cấp quản lý
                    case "ManagementLevel":
                        txtManagementLevel.Text = dr["Parameter_Value"].ToString();
                        txtManagementLevel.Tag = dr["Parameter_ID"];
                        break;
                    // 11. Mức lương tối thiếu
                    case "MinBasicSalary":
                        txtMinBasicSalary.Text = Convert.ToDecimal(dr["Parameter_Value"]).ToString(HRM.UIParamater.Phay2So);
                        txtMinBasicSalary.Tag = dr["Parameter_ID"];
                        break;
                    // 12. Tiền giảm trừ gia cảnh:
                    case "FamilySalary":
                        txtFamilySalary.Text = Convert.ToDecimal(dr["Parameter_Value"]).ToString(HRM.UIParamater.Phay2So);
                        txtFamilySalary.Tag = dr["Parameter_ID"];
                        break;
                    // 13. Tỉ lệ đóng BHYT:
                    case "MedicalInsurancePercent":
                        txtMedicalInsurancePercent.Text = dr["Parameter_Value"].ToString();
                        txtMedicalInsurancePercent.Tag = dr["Parameter_ID"];
                        break;
                    // 14. Tỉ lệ đóng BHXH:
                    case "SocialInsurancePercent":
                        txtSocialInsurancePercent.Text = dr["Parameter_Value"].ToString();
                        txtSocialInsurancePercent.Tag = dr["Parameter_ID"];
                        break;
                    // 15. Số ngày làm việc trong tháng 
                    case "TotalDateWork":
                        txtTotalDateWork.Text = dr["Parameter_Value"].ToString();
                        txtTotalDateWork.Tag = dr["Parameter_ID"];
                        break;
                    // 16. Tỉ lệ đóng BHYT NLĐ:
                    case "MedicalInsurancePercentLabour":
                        txtMedicalInsurancePercentLabour.Text = dr["Parameter_Value"].ToString();
                        txtMedicalInsurancePercentLabour.Tag = dr["Parameter_ID"];
                        break;
                    // 17. Tỉ lệ đóng BHXH NLĐ:
                    case "SocialInsurancePercentLabour":
                        txtSocialInsurancePercentLabour.Text = dr["Parameter_Value"].ToString();
                        txtSocialInsurancePercentLabour.Tag = dr["Parameter_ID"];
                        break;
                    // 18. Ngày chốt sổ lương:
                    case "CloseBook":
                        txtCloseBook.Text = dr["Parameter_Value"].ToString();
                        txtCloseBook.Tag = dr["Parameter_ID"];
                        break;
                    // 19. Ngày vào công ty để được tính lương trong tháng hay tính sang tháng sau:
                    case "Joining":
                        txtJoining.Text = dr["Parameter_Value"].ToString();
                        txtJoining.Tag = dr["Parameter_ID"];
                        break;
                    case "UnemployedIns":
                        txtUnemployedIns.Text = dr["Parameter_Value"].ToString();
                        txtUnemployedIns.Tag = dr["Parameter_ID"];
                        break;
                    case "UnemployedInsCompany":
                        txtUnemployedInsCompany.Text = dr["Parameter_Value"].ToString();
                        txtUnemployedInsCompany.Tag = dr["Parameter_ID"];
                        break;
                    case "TradeUnion":
                        txtTradeUnion.Text = dr["Parameter_Value"].ToString();
                        txtTradeUnion.Tag = dr["Parameter_ID"];
                        break;
                }
            }
            isChange = false;
        }
        private bool CheckValid()
        {
            if (CheckTextValid.IsEmpty(txtCompanyName.Text))
            {
                MessageBox.Show(UpdateUI.GetString("tendonvitrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCompanyName.Focus();
                return false;
            }
            if (CheckTextValid.IsEmpty(txtCompanyAddress.Text))
            {
                MessageBox.Show(UpdateUI.GetString("diachidonvitrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCompanyAddress.Focus();
                return false;
            }
            if (CheckTextValid.IsEmpty(txtPhone.Text))
            {
                MessageBox.Show(UpdateUI.GetString("dienthoaitrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCompanyAddress.Focus();
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtMinBasicSalary.Text))
            {
                MessageBox.Show("Mức lương tối thiểu không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtMinBasicSalary.Focus();
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtFamilySalary.Text))
            {
                MessageBox.Show("Tiền giảm trừ gia cảnh không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtFamilySalary.Focus();
                return false;
            }
            return true;

        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            bool isSuccess = true;
            foreach (Control ctl in this.panel2.Controls)
            {
                if (ctl is TextBox)
                {
                    _parameterInfo.ID = 0;
                    if (ctl.Tag.ToString().Length > 0)
                        _parameterInfo.ID = Convert.ToInt32(ctl.Tag);

                    _parameterInfo.Name = ctl.Name.Substring(3);
                    if (((TextBox)ctl).TextAlign == HorizontalAlignment.Right)
                    {
                        _parameterInfo.Value = Convert.ToDouble(ctl.Text).ToString();
                    }
                    else
                    {
                        _parameterInfo.Value = ctl.Text;
                    }
                    if (_parameterInfo.ID == 0)
                    {
                        _parameterInfo.ID = _wcfParameter.Parameter_Insert(_parameterInfo);
                        if (_parameterInfo.ID > 0)
                            ctl.Tag = _parameterInfo.ID;
                        else
                            isSuccess = false;
                    }
                    else
                    {
                        if (!_wcfParameter.Parameter_Update(_parameterInfo))
                            isSuccess = false;
                    }
                }
            }

            if (isSuccess)
            {

                UIParamater.CompanyName = txtCompanyName.Text;
                Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                isChange = false;
            }
            else
            {
                MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                isChange = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmParameter_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F3:
                    if (btnSave.Visible)
                        btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void frmParameter_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
        }

        private void DataChanged(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void frmParameter_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"),UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
        }

        private void txtMinBasicSalary_Leave(object sender, EventArgs e)
        {
            txtMinBasicSalary.Text = UISetting.GetDecimalNo(txtMinBasicSalary.Text).ToString(HRM.UIParamater.Phay2So);
        }

        private void txtFamilySalary_Leave(object sender, EventArgs e)
        {
            txtFamilySalary.Text = UISetting.GetDecimalNo(txtFamilySalary.Text).ToString(HRM.UIParamater.Phay2So);
        }

        private void txtMedicalInsurancePercent_Leave(object sender, EventArgs e)
        {

        }

        private void txtMedicalInsurancePercentLabour_Leave(object sender, EventArgs e)
        {

        }

        private void txtSocialInsurancePercent_Leave(object sender, EventArgs e)
        {

        }

        private void txtSocialInsurancePercentLabour_Leave(object sender, EventArgs e)
        {

        }

        private void txtUnemployedInsCompany_Leave(object sender, EventArgs e)
        {

        }

        private void txtUnemployedIns_Leave(object sender, EventArgs e)
        {

        }

        private void txtTotalDateWork_Leave(object sender, EventArgs e)
        {

        }

        private void txtTradeUnion_Leave(object sender, EventArgs e)
        {

        }

        private void txtCloseBook_Leave(object sender, EventArgs e)
        {

        }

        private void txtJoining_Leave(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
